﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Andrewl.XnaHelpers.Audio;
using Andrewl.XnaHelpers.Input;
using Andrewl.XnaHelpers.Storage;
using Andrewl.XnaHelpers.Guide_Helper;

namespace Andrewl.XnaHelpers
{
    public class XnaHelpersComponent : Microsoft.Xna.Framework.GameComponent
    {
        private GraphicsDeviceManager deviceManager;

        private string name;

        private bool UpdateBasedOnPlatform;
        private bool UpdateKeyboard;
        private bool UpdateMouse;
        private bool UpdateGamepad;

        public XnaHelpersComponent(string GameName, Game game, GraphicsDeviceManager GraphicsDeviceManager)
            : base(game)
        {
            deviceManager = GraphicsDeviceManager;
            UpdateBasedOnPlatform = true;
            name = GameName;
        }

        /// <summary>
        /// Adds the XnaHelpersComponent along with the FrameRateCounter component that will be drawn
        /// with the specified color
        /// </summary>
        /// <param name="game"></param>
        /// <param name="FrameRateCounterColor"></param>
        public XnaHelpersComponent(string GameName, Game game, GraphicsDeviceManager GraphicsDeviceManager, Color FrameRateCounterColor)
            : base(game)
        {
            deviceManager = GraphicsDeviceManager;
            Game.Components.Add(new FrameRateCounter(game, FrameRateCounterColor));
            UpdateBasedOnPlatform = true;
            name = GameName;
        }

        public XnaHelpersComponent(string GameName, Game game, GraphicsDeviceManager GraphicsDeviceManager, bool UpdateKeyboardArg, bool UpdateMouseArg, bool UpdateGamePadArg)
            : base(game)
        {
            name = GameName;
            deviceManager = GraphicsDeviceManager;

            this.UpdateKeyboard = UpdateKeyboardArg;
            this.UpdateMouse = UpdateMouseArg;
            this.UpdateGamepad = UpdateGamePadArg;
            this.UpdateBasedOnPlatform = false;
        }

        private void Startup()
        {
            XnaHelpers.name = name;
            XnaHelpers.game = Game;
            XnaHelpers.graphicsManager = deviceManager;
            XnaHelpers.graphicsDevice = Game.GraphicsDevice;
            XnaHelpers.content = Game.Content;
            XnaHelpers.resxContent = new ResourceContentManager(Game.Services, embed.ResourceManager);
            XnaHelpers.spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            XnaHelpers.gameTime = new GameTime(); //is set in the Update method
            XnaHelpers.inputManager = new InputManager();
#if XBOX
            XnaHelpers.storage = new XboxStorage();
#endif
#if WINDOWS
            XnaHelpers.storage = new WindowsStorage();
#endif
            Texture2D pixle = new Texture2D(Game.GraphicsDevice, 1, 1);
            pixle.SetData<Color>(new Color[] { Color.White });
            XnaHelpers.whitePixel = pixle;
#if WINDOWS
            XnaHelpers.debugFont = XnaHelpers.resxContent.Load<SpriteFont>("DebugFont_x86");
#endif
#if XBOX
            XnaHelpers.debugFont = XnaHelpers.resxContent.Load<SpriteFont>("DebugFont_xbox");
#endif
            XnaHelpers.xactSoundManagers = new XactAudio();
            XnaHelpers.standardSoundManagers = new StandardAudio();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            Startup();
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            XnaHelpers.gameTime = gameTime;

            #region Audio

            if (XnaHelpers.XactAudio.xactSoundManagers.Count > 0)
            {
                foreach (XactSoundManager xsm in XnaHelpers.XactAudio.xactSoundManagers)
                {
                    xsm.Update();
                }
            }
            #endregion
            #region Input
            if (UpdateBasedOnPlatform)
            {
#if WINDOWS
                XnaHelpers.Input.UpdateKeyboard();
                XnaHelpers.Input.UpdateMouse();
#endif
#if XBOX
                XnaHelpers.Input.UpdateGamePad();
                XnaHelpers.Input.UpdateChatPad();
#endif
            }
            else
            {
                if (UpdateKeyboard)
                    XnaHelpers.Input.UpdateKeyboard();
                if (UpdateMouse)
                    XnaHelpers.Input.UpdateMouse();
                if (UpdateGamepad)
                {
                    XnaHelpers.Input.UpdateGamePad();
                    XnaHelpers.Input.UpdateChatPad();
                }
            }
            #endregion
            #region GuideHelper

            GuideHelper.Update();

            #endregion

            base.Update(gameTime);
        }

    }

}